<?php
declare(strict_types=1);

namespace app\shop\service;

use app\shop\enums\payment\OrderType;
use app\shop\model\PaymentTradeModel;

class PaymentTradeService
{
    private PaymentTradeModel $model;

    public function __construct()
    {
        $this->model = new PaymentTradeModel();
    }

    public function queryShopOrder(int|string $orderId)
    {
        return $this->model->where([
            ['order_type', '=', OrderType::SHOP],
            ['order_id', '=', $orderId]
        ])->findOrEmpty();
    }

    public function queryRechargeOrder(int|string $orderId)
    {
        return $this->model->where([
            ['order_type', '=', OrderType::RECHARGE],
            ['order_id', '=', $orderId]
        ])->findOrEmpty();
    }

    public function updateTradeStatus(string $outTradeNo, $status): PaymentTradeModel
    {
        return $this->model->where('out_trade_no', $outTradeNo)->update([
            'trade_state' => $status
        ]);
    }

    public function getPaymentById(int|string $id)
    {
        return $this->model->where('id', $id)->findOrEmpty();
    }
}